package com.meihua.baoleyi.fragment.sickness;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseFragment;
import com.meihua.baoleyi.bean.ManipulationPlanBean;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.ui.ManipulationActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 推拿方案
 * Created by liming on 2017/10/19.
 */

public class TherapManipulationFragment extends BaseFragment implements View.OnClickListener {

    @Bind(R.id.rv_content)
    RecyclerView mRecyclerView;
    private  Context mcontext;
    private MyAdapter mAdapter;

    private List<ManipulationPlanBean.DataBean.AcupointBean> dataList = new ArrayList<>();

    public static TherapManipulationFragment newInstance(String json) {
        TherapManipulationFragment fragment = new TherapManipulationFragment();
        Bundle mBundle = new Bundle();
        mBundle.putString("json", json);
        fragment.setArguments(mBundle);
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mcontext = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.therap_fragment2, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        String json = getArguments().getString("json");
        ManipulationPlanBean bean = NetHelper.getBeanForJson(ManipulationPlanBean.class, json);
        dataList.clear();
        dataList.addAll(bean.getData().getAcupoint());

        mAdapter = new MyAdapter();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mcontext));
        mRecyclerView.setAdapter(mAdapter);
    }

    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
        public MyAdapter() {
            super();
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((MyViewHolder)holder).displayData(dataList.get(position));
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_manipulation, parent, false);
            return new MyViewHolder(v);
        }

        @Override
        public int getItemCount() {
            return dataList.size()>0 ? dataList.size(): 0;
        }

        class MyViewHolder extends RecyclerView.ViewHolder{
            private RelativeLayout item;
            private TextView name;

            public MyViewHolder(View itemView) {
                super(itemView);
                item = itemView.findViewById(R.id.rlt_item);
                name = itemView.findViewById(R.id.tv_name);
            }

            public void displayData(final ManipulationPlanBean.DataBean.AcupointBean data) {
                name.setText(data.getPoint_name());
                item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(ManipulationActivity.createIntent(mContext, data.getId()));
                    }
                });
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(mcontext);
    }
}
